--
-- Author: shunguo.chen
-- Date: 2017-12-18 16:35:14
--
-- MountAttrView  兽栏总属性界面

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.MountAttrScene

local MID_POSX = 85

function ClassRef:init()
    self._ui.Button_enter:addClickEventListener(function()
        executeFunc(self._onCloseView, self)
    end)

    local dataPanel = self._ui.Panel_content:getChildByName("Panel_data")
    self._attrNodes = {0,0,0,0,0,0}
    for i=1,6 do
        self._attrNodes[i] = dataPanel:getChildByName("data_" .. i)
        self._attrNodes[i]:setVisible(false)
    end

    self._list = self._ui.Panel_content:getChildByName("ListView")
    local item = self._list:getItem(0)
    self._list:setItemModel(item)
    self._width = item:getContentSize().width
end

function ClassRef:refresh(attrs, skills)
    local len = #skills
    self._ui.Panel_none:setVisible(len == 0)
    self._ui.Panel_content:setVisible(len > 0)
    if len > 0 then
        local maxAttr = 0
        for k,v in pairs(attrs) do
            maxAttr = maxAttr + 1
        end
        local index = 1
        for k,v in pairs(attrs) do
            if index <= 6 then
                local node = self._attrNodes[index]
                index = index + 1
                node:setVisible(true)
                local name, value = GD:getPropShowData(k, v)
                node:findChild("Panel/data_tlt"):setString(name)
                node:findChild("Panel/data_num"):setString("+" .. value)

                if maxAttr <= 3 then
                    node:setPositionX(MID_POSX)
                end
            else
                break
            end
        end

        for i,v in ipairs(skills) do
            if i > 1 then
                self._list:pushBackDefaultItem()
            end

            local text = self._list:getItem(i-1)
            text:setTextAreaSize(cc.size(self._width, 0))
            text:setString(skills[i])

            local renderSize = text:getVirtualRendererSize()
            text:setContentSize(renderSize)
        end
    end
end

return ClassRef